package web.controller.employer;

import common.consts.Consts;
import common.consts.ContentType;
import common.validation.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.RecruitmentService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.RecruitmentRequest;
import web.security.Protection;

/**
 * Created by aceyin on 15/6/24.
 */
@RestController("employerRecruitment")
@RequestMapping(Routes.Employer.RECRUITMENT)
public class Recruitment extends Base {
    private static final Logger LOGGER = LoggerFactory.getLogger(Recruitment.class);

    @Autowired
    private RecruitmentService recruitmentService;

    @Protection
    @RequestMapping(method = RequestMethod.GET, produces = ContentType.JSON)
    public ServiceResponse view(@RequestParam("id") long id) {
        if (id <= 0) {
            return fail(Consts.i18nKey.INVALID_RECRUITMENT_ID);
        }

        long employer = getUserIdFromAuthHeader();

        return recruitmentService.getRecruitment4Employer(employer, id);
    }

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse save(@RequestBody RecruitmentRequest request) {
        ValidationResult result = request.validate();
        if (result.isFailed()) {
            return fail(result.getMessage());
        }

        long employer = getUserIdFromAuthHeader();

        return recruitmentService.saveRecruitment(employer, request.getRecruitment4Employer());
    }
}
